package model

import (
	"errors"
	"gorm.io/gorm"
	"ppa/init/mysql"
)

type Collection struct {
	ID uint `json:"id"  form:"collection_id" gorm:"primaryKey;autoIncrement"`
	CollectionName string `json:"collection_name" form:"collection_name" gorm:"default:'';type:varchar(50);"`
	CollectionImages string `json:"collection_images" form:"collection_images" gorm:"default:'';type:varchar(255);"`
	CollectionAddress string `form:"collection_address" json:"collection_address"     gorm:"unique;type:varchar(42);"`
	UserId uint `json:"user_id" form:"user_id"   gorm:"type:int(10);"`
	ShortUrl string `json:"short_url" form:"short_url" gorm:"default:'';type:varchar(10);"`
	TokenSymbol string `json:"token_symbol" form:"token_symbol" gorm:"default:'';type:varchar(10);"`
	User User `json:"user" gorm:"ForeignKey:UserId;AssociationForeignKey:UserId"`
}

// GetCollectionInfo 查询合集的详细信息
func (collection *Collection)GetCollectionInfo() (*Collection, error)  {
	var collectionInfo *Collection
	err := mysql.SqlDB.Preload("User").Where(collection).First(&collectionInfo).Error
	if err != nil || errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("未查询到数据存在")
	}
	return collectionInfo, nil
}

// GetCollectionNewList 获取最新的合集列表
func (collection *Collection)GetCollectionNewList() (*[]Collection, error)  {
	var collectionInfo *[]Collection
	err := mysql.SqlDB.Preload("User").Order("id desc").Find(&collectionInfo).Error
	if err != nil || errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("未查询到数据存在")
	}
	return collectionInfo, nil
}

// GetCollectionListByWhere 根据条件查询合集列表
func (collection *Collection)GetCollectionListByWhere() (*[]Collection, error)  {
	var collectionInfo *[]Collection
	err := mysql.SqlDB.Where(collection).Order("id desc").Find(&collectionInfo).Error
	if err != nil || errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("未查询到数据存在")
	}
	return collectionInfo, nil
}

// SetCollectionInfo 添加一个新的合集
func(collection *Collection)SetCollectionInfo() (*Collection, error)  {
	err := mysql.SqlDB.Create(&collection).Error
	if err != nil {
		return nil, err
	}
	return nil, nil
}
